home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / MW MPW Binaries 1.1.1a2 / mwcPPC / MWCExamples / Count ƒ / Count.c next >
Encoding:
C/C++ Source or Header  |  1994-06-13  |  3.6 KB  |  157 lines  |  [TEXT/MPS ]

  1. /*------------------------------------------------------------------------------
  2.  
  3. NAME
  4.       Count -- count lines and characters
  5.  
  6. SYNOPSIS
  7.       Count [-l] [-c] [file…]
  8.  
  9. DESCRIPTION
  10.       "Count" counts the lines and characters in its input, and writes the
  11.         counts to standard output.  If no files are specified standard input is
  12.         read.  If more than one file is specified, separate counts are written
  13.         for each file, one per line, preceeded by the file name.  A total is also
  14.         written following the list of files.
  15.         
  16. COPYRIGHT
  17.       Copyright Apple Computer, Inc. 1985-1988, 1994
  18.         All rights reserved.
  19.  
  20. ------------------------------------------------------------------------------*/
  21.  
  22. #include    <Types.h>
  23. #include     <ctype.h>
  24. #include     <fcntl.h>
  25. #include     <string.h>
  26. #include     <stdio.h>
  27. #include    <ErrMgr.h>
  28. #include    <CursorCtl.h>
  29. #include    <Errors.h>
  30.  
  31.  
  32. #define    InputSize    (1024*8)
  33.  
  34. /* Variables local to this file */
  35.  
  36. static    char    inputBuffer[InputSize + 1];
  37. static    char    errorBuffer[256];
  38.  
  39. static    char    *usage = "# Usage - %s [-l] [-c] [file…].\n";
  40. static    long    optionsSpecified;
  41. static    long    lineOption;
  42. static    long    charOption;
  43.  
  44. struct counts {
  45.     long    lines;
  46.     long    characters;
  47. };
  48.  
  49. struct counts count(FILE *input)
  50. {
  51.     char                    c;
  52.     char                    *ptr;
  53.     long                    lines = 0;
  54.     long                    characters = 0;
  55.     long                    charsRead;
  56.     struct    counts    cnts;
  57.     
  58.     while ((charsRead = fread(inputBuffer, sizeof(char), InputSize, input)) > 0) {
  59.         ptr = inputBuffer;
  60.         inputBuffer[charsRead] = 0;
  61.         characters += charsRead;
  62.         while ((c = *ptr++) != 0 || ptr <= &inputBuffer[charsRead]) {
  63.             if (c == '\n') {
  64.                 lines++;
  65.                 if ((lines & 0x0F) == 0)
  66.                     SpinCursor(1);
  67.             }
  68.         }
  69.     }    
  70.     if (characters > 0 && *(ptr-2) != '\n')
  71.         lines++;
  72.     cnts.lines = lines;
  73.     cnts.characters = characters;
  74.     return cnts;
  75. }
  76.  
  77.  
  78. void print(long files, long max, char *name, struct counts cnts)
  79. {
  80.     long    space;
  81.     
  82.     space = 0;
  83.     if (files > 1) {
  84.         fprintf(stdout,"%s ", name);
  85.         space = max - strlen(name);
  86.     }
  87.     if (optionsSpecified == false || lineOption == true) {
  88.         fprintf(stdout, "%*d ", space + 5, cnts.lines);
  89.         space = 0;
  90.     }
  91.     if (optionsSpecified == false || charOption == true) {
  92.         fprintf(stdout, "%*d ", space + 7, cnts.characters);
  93.     }
  94.     fprintf(stdout, "\n");
  95.     fflush(stdout);
  96. }
  97.         
  98.  
  99. main(int argc, char *argv[])
  100. {
  101.    long                status;
  102.     long            parms;
  103.     long            files;
  104.     long            done;
  105.     long            length;
  106.     long            max;
  107.     FILE            *input;
  108.     struct    counts    cnts;
  109.     struct    counts    total;
  110.  
  111.     status = files = 0;
  112.     max = strlen("Total");
  113.     optionsSpecified = lineOption = charOption = false;
  114.     InitCursorCtl(nil);
  115.  
  116.     for (parms = 1; parms < argc; parms++) {
  117.         length = strlen(argv[parms]);
  118.         if (*argv[parms] != '-') {
  119.             argv[++files] = argv[parms];
  120.             if (max < length)
  121.                 max = length;
  122.         } else if (tolower(*(argv[parms]+1)) == 'c' && length == 2) {
  123.             optionsSpecified = charOption = true;
  124.         } else if (tolower(*(argv[parms]+1)) == 'l' && length == 2) {
  125.             optionsSpecified = lineOption = true;
  126.         } else {
  127.             fprintf(stderr,"### %s - \"%s\" is not an option.\n", argv[0], argv[parms]);
  128.             fprintf(stderr, usage, argv[0]);
  129.             return 1;
  130.         }
  131.     }
  132.     if (files == 0) {
  133.         cnts = count(stdin);
  134.         print(files, max, NULL, cnts);
  135.     } else {
  136.         total.lines = total.characters = done = 0;
  137.         for (parms = 1; parms <= files; parms++) {
  138.             if ((input = fopen(argv[parms], "r")) != NULL) {
  139.                 cnts = count(input);
  140.                 fclose(input);
  141.                 total.lines += cnts.lines;
  142.                 total.characters += cnts.characters;
  143.                 print(files, max, argv[parms], cnts);
  144.                 done++;
  145.             } else {
  146.                 fprintf(stderr,"### %s - Unable to open file %s.\n", argv[0], argv[parms]);
  147.                 fprintf(stderr,"# %s\n", GetSysErrText(MacOSErr, errorBuffer));
  148.                 status = 2;
  149.             }
  150.         }
  151.         if (done > 1) {
  152.             print(files,max,"Total",total);
  153.         }
  154.     }
  155.     return status;
  156. }
  157.